#include "L2DataSet.hpp"
#include <cstdio>
#include <cstdlib>

int malloc_count = 0;
int free_count = 0;

class MyMemAllocator : public DataSet::MemAllocator {
public:
    virtual void *malloc(size_t size){
        printf("[I][DataSet] malloc(%d) size %d\r\n",malloc_count++,size);
        return ::malloc(size);
    }

    virtual void free(void *mem){
        printf("[I][DataSet] free(%d) address %p\r\n",free_count++,mem);
        return ::free(mem);
    }

};

DataSet dataset;
MyMemAllocator my_mem_allocator;

int main(){
    char label_buffer[128];
    dataset.setMemAllocator(&my_mem_allocator);
    /* 添加布尔类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.boolean.%d",n);
        dataset.add(label_buffer,(n % 2 == 0) ? true : false);
    }
    /* 添加数值类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.number.%d",n);
        dataset.add(label_buffer,(double)n);
    }
    /* 添加字符串类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.string.%d",n);
        dataset.add(label_buffer,label_buffer);
    }
    /* 打印布尔类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.boolean.%d",n);
        printf("%s=%s\r\n",label_buffer,dataset.getBoolean(label_buffer) ? "true" : "false");
    }
    /* 打印数值类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.number.%d",n);
        printf("%s=%f\r\n",label_buffer,dataset.getNumber(label_buffer));
    }
    /* 打印字符串类型数据. */
    for ( int n = 0; n < 10; n++ ){
        snprintf(label_buffer,sizeof(label_buffer) - 1,"data.string.%d",n);
        printf("%s=%s\r\n",label_buffer,dataset.getString(label_buffer));
    }

    return 0;
}